package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	
	public class FrameStrengthen extends JFrame
	{
		//members define
		private var labelName:JLabel;
		private var labelLevel:JLabel;
		private var panelTabs:JPanel;
		private var btnHP:JToggleButton;
		private var btnAP:JToggleButton;
		private var btnDP:JToggleButton;
		private var btnSP:JToggleButton;
		private var labelOld:JLabel;
		private var labelNew:JLabel;
		private var labelRequireMoney:JLabel;
		private var btnStrengthen:JButton;
		private var labelCount:JLabel;
		private var panelHead:JPanel;
		//----------------------------------
		private var btn_close:JButton;
		public function FrameStrengthen()
		{
			setClosable(false);
			setResizable(false);  
			setBorder(new EmptyBorder());
			
			btn_close = new JButton();
			btn_close.setLocation(new IntPoint(82, 0));
			btn_close.setSize(new IntDimension(27, 27));
			btn_close.setPreferredSize(new IntDimension(27, 27));
			this.getTitleBar().setCloseButton(btn_close);
			this.getTitleBar().setMinimizeHeight(32);
			
			
			//component creation
			setSize(new IntDimension(170, 193));
			setPreferredSize(new IntDimension(170, 193));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			labelName = new JLabel();
			labelName.setLocation(new IntPoint(62, 7));
			labelName.setSize(new IntDimension(100, 16));
			labelName.setPreferredSize(new IntDimension(100, 16));
			labelName.setText("label");
			
			labelLevel = new JLabel();
			labelLevel.setLocation(new IntPoint(62, 30));
			labelLevel.setSize(new IntDimension(100, 16));
			labelLevel.setPreferredSize(new IntDimension(100, 16));
			labelLevel.setText("123");
			
			panelTabs = new JPanel();
			panelTabs.setLocation(new IntPoint(13, 59));
			panelTabs.setSize(new IntDimension(148, 21));
			panelTabs.setPreferredSize(new IntDimension(148, 21));
			var layout1:FlowLayout = new FlowLayout();
			layout1.setAlignment(AsWingConstants.CENTER);
			layout1.setHgap(0);
			layout1.setVgap(0);
			panelTabs.setLayout(layout1);
			
			btnHP = new JToggleButton();
			btnHP.setLocation(new IntPoint(5, 0));
			btnHP.setSize(new IntDimension(37, 29));
			btnHP.setPreferredSize(new IntDimension(37, 21));
			btnHP.setText("label");
			
			btnAP = new JToggleButton();
			btnAP.setLocation(new IntPoint(50, 0));
			btnAP.setSize(new IntDimension(37, 29));
			btnAP.setPreferredSize(new IntDimension(37, 21));
			btnAP.setText("label");
			
			btnDP = new JToggleButton();
			btnDP.setLocation(new IntPoint(126, 3));
			btnDP.setSize(new IntDimension(31, 22));
			btnDP.setPreferredSize(new IntDimension(37, 21));
			btnDP.setText("label");
			
			btnSP = new JToggleButton();
			btnSP.setLocation(new IntPoint(131, 0));
			btnSP.setSize(new IntDimension(37, 21));
			btnSP.setPreferredSize(new IntDimension(37, 21));
			btnSP.setText("label");
			
			labelOld = new JLabel();
			labelOld.setLocation(new IntPoint(13, 83));
			labelOld.setSize(new IntDimension(55, 17));
			labelOld.setPreferredSize(new IntDimension(55, 17));
			labelOld.setText("25");
			
			labelNew = new JLabel();
			labelNew.setLocation(new IntPoint(103, 83));
			labelNew.setSize(new IntDimension(55, 17));
			labelNew.setPreferredSize(new IntDimension(55, 17));
			labelNew.setText("26");
			
			labelRequireMoney = new JLabel();
			labelRequireMoney.setLocation(new IntPoint(73, 129));
			labelRequireMoney.setSize(new IntDimension(55, 17));
			labelRequireMoney.setPreferredSize(new IntDimension(55, 17));
			labelRequireMoney.setText("256");
			
			btnStrengthen = new JButton();
			btnStrengthen.setLocation(new IntPoint(59, 152));
			btnStrengthen.setSize(new IntDimension(52, 27));
			btnStrengthen.setPreferredSize(new IntDimension(52, 27));
			btnStrengthen.setText("强化");
			
			labelCount = new JLabel();
			labelCount.setLocation(new IntPoint(73, 106));
			labelCount.setSize(new IntDimension(55, 17));
			labelCount.setPreferredSize(new IntDimension(55, 17));
			labelCount.setText("label");
			labelCount.setHorizontalAlignment(AsWingConstants.CENTER);
			
			panelHead = new JPanel();
			panelHead.setLocation(new IntPoint(12, 5));
			panelHead.setSize(new IntDimension(47, 48));
			panelHead.setPreferredSize(new IntDimension(47, 48));
			
			//component layoution
			getContentPane().append(labelName);
			getContentPane().append(labelLevel);
			getContentPane().append(panelTabs);
			getContentPane().append(labelOld);
			getContentPane().append(labelNew);
			getContentPane().append(labelRequireMoney);
			getContentPane().append(btnStrengthen);
			getContentPane().append(labelCount);
			getContentPane().append(panelHead);
			
			panelTabs.append(btnHP);
			panelTabs.append(btnAP);
			panelTabs.append(btnDP);
			panelTabs.append(btnSP);
			//------------------------------------------------
			
			
			
			setImages();
			btnHP.addActionListener(onBtnHP);
			btnAP.addActionListener(onBtnAP);
			btnDP.addActionListener(onBtnDP);
			btnSP.addActionListener(onBtnSP);
			btnStrengthen.addActionListener(onBtnStrengthen);
			//this.addEventListener(FrameEvent.FRAME_CLOSING,onFrameClosing);
			addEventListener(FrameEvent.FRAME_CLOSING,onFrameClosing);
			addEventListener(AWEvent.HIDDEN,onFrameClosing);
		}
		private function onFrameClosing(e:Event):void
		{
			var _frameDest:JFrame = _strengthen_type == STRENGTHEN_TYPE_CHAR ?FrameChar._frame:FramePet._frame;
			var teamDestX:int = (Engine.W - _frameDest.width)/2;
			var teamDestY:int = (Engine.H - _frameDest.height)/2;
			var tween:TweenLite = TweenLite.to(_frameDest, 0.3, {x:teamDestX, y:teamDestY});
			
		}

		private function onBtnHP(e:Event):void
		{
			this.paintImmediately();
			_strengthen_p = STRENGTHEN_HP;
			update();
		}
		private function onBtnAP(e:Event):void
		{
			this.paintImmediately();
			_strengthen_p = STRENGTHEN_AP;
			update();
		}
		private function onBtnDP(e:Event):void
		{
			this.paintImmediately();
			_strengthen_p = STRENGTHEN_DP;
			update();
		}
		private function onBtnSP(e:Event):void
		{
			this.paintImmediately();
			_strengthen_p = STRENGTHEN_SP;
			update();
		}
		private function onBtnStrengthen(e:Event):void
		{
			//PanelMain.showInfoTimeLimit("此功能暂未开放");
			if(int(labelRequireMoney.getText()) > CharInfo.money)
			{
				PanelMain.showInfoTimeLimit("铜币不足,无法强化!");
				return;
			}
			var _tmpHPAdd:int = 0;
			var _tmpAPAdd:int = 0;
			var _tmpDPAdd:int = 0;
			var _tmpSPAdd:int = 0;
			
			if(_strengthen_type == STRENGTHEN_TYPE_CHAR)
			{
				if(_strengthen_p == STRENGTHEN_HP)
				{
					_tmpHPAdd = 1;
				}
				if(_strengthen_p == STRENGTHEN_AP)
				{
					_tmpAPAdd = 1;
				}
				if(_strengthen_p == STRENGTHEN_DP)
				{
					_tmpDPAdd = 1;
				}
				if(_strengthen_p == STRENGTHEN_SP)
				{
					_tmpSPAdd = 1;
				}
				Client.getInstance().sendPacketLogic(22,[_tmpHPAdd,_tmpAPAdd,_tmpDPAdd,_tmpSPAdd]);
			}
			if(_strengthen_type == STRENGTHEN_TYPE_PET)
			{

				var object:Object = CharInfo.m_arPets[_pet_index];
				if(object == null)
					return;
				if(_strengthen_p == STRENGTHEN_HP)
				{
					_tmpHPAdd = 1;
				}
				if(_strengthen_p == STRENGTHEN_AP)
				{
					_tmpAPAdd = 1;
				}
				if(_strengthen_p == STRENGTHEN_DP)
				{
					_tmpDPAdd = 1;
				}
				if(_strengthen_p == STRENGTHEN_SP)
				{
					_tmpSPAdd = 1;
				}
				
				Client.getInstance().sendPacketLogic(105,[_pet_index,int(CharInfo.m_arPets[_pet_index].id),_tmpHPAdd,_tmpAPAdd,_tmpDPAdd,_tmpSPAdd]);
			}
			update();
		}
		public function update():void
		{
			var p:int = 0;
			if(_strengthen_type == STRENGTHEN_TYPE_CHAR)
			{
				if(_strengthen_p == STRENGTHEN_HP)
				{
					labelOld.setText(CharInfo.hpMax.toString());
					labelNew.setText(CharInfo.m_nNextPointHP.toString());
					labelRequireMoney.setText(CharInfo.m_nNextPointMoneyHP.toString());
					p = CharInfo.pointHP;
				}
				if(_strengthen_p == STRENGTHEN_AP)
				{
					labelOld.setText(CharInfo.ap.toString());
					labelNew.setText(CharInfo.m_nNextPointAP.toString());
					labelRequireMoney.setText(CharInfo.m_nNextPointMoneyAP.toString());
					p = CharInfo.pointAP;
				}
				if(_strengthen_p == STRENGTHEN_DP)
				{
					labelOld.setText(CharInfo.dp.toString());
					labelNew.setText(CharInfo.m_nNextPointDP.toString());
					labelRequireMoney.setText(CharInfo.m_nNextPointMoneyDP.toString());
					p = CharInfo.pointDP;
				}
				if(_strengthen_p == STRENGTHEN_SP)
				{
					labelOld.setText(CharInfo.sp.toString());
					labelNew.setText(CharInfo.m_nNextPointSP.toString());
					labelRequireMoney.setText(CharInfo.m_nNextPointMoneySP.toString());
					p = CharInfo.pointSP;
				}
				labelCount.setText(p+'/' + CharInfo.m_nLevel * 6);
				//labelRequireMoney.setText(getStrengthenMoney(p).toString());
				btnStrengthen.setEnabled(p < CharInfo.m_nLevel * 6);
			}
			if(_strengthen_type == STRENGTHEN_TYPE_PET)
			{
				var object:Object = CharInfo.m_arPets[_pet_index];
				if(object == null)
					return;
				if(_strengthen_p == STRENGTHEN_HP)
				{
					labelOld.setText(object.hp_max.toString());
					labelNew.setText(object.m_nNextPointHP.toString());
					labelRequireMoney.setText(object.m_nNextPointMoneyHP.toString());
					p = object.m_nPointHP;
				}
				if(_strengthen_p == STRENGTHEN_AP)
				{
					labelOld.setText(object.ap.toString());
					labelNew.setText(object.m_nNextPointAP.toString());
					labelRequireMoney.setText(object.m_nNextPointMoneyAP.toString());
					p = object.m_nPointAP;
				}
				if(_strengthen_p == STRENGTHEN_DP)
				{
					labelOld.setText(object.dp.toString());
					labelNew.setText(object.m_nNextPointDP.toString());
					labelRequireMoney.setText(object.m_nNextPointMoneyDP.toString());
					p = object.m_nPointDP;
				}
				if(_strengthen_p == STRENGTHEN_SP)
				{
					labelOld.setText(object.sp.toString());
					labelNew.setText(object.m_nNextPointSP.toString());
					labelRequireMoney.setText(object.m_nNextPointMoneySP.toString());
					p = object.m_nPointSP;
				}
				labelCount.setText(p+'/' + object.m_nLevel * 6);
				//labelRequireMoney.setText(getStrengthenMoney(p).toString());
				btnStrengthen.setEnabled(p < object.m_nLevel * 6);
			}
			labelName.setText(_name);
			labelLevel.setText('Lv.' + _level.toString());
			
			
		}
		
		public function setImages():void
		{
			btn_close.wrapSimpleButton(Client.getUIElement("btn_close") as SimpleButton);
			var img_background:DisplayObject = Client.getUIElement("img_strengthen_bg") as DisplayObject;
			setBackgroundDecorator(new AssetBackground(img_background));
			setSize(new IntDimension(img_background.width, img_background.height));
			setPreferredSize(new IntDimension(img_background.width, img_background.height));
			pack();
			setLocationXY((Engine.W - width) / 2,(Engine.H - height)/2);
			
			panelHead.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_head") as DisplayObject));
			
			btnHP.wrapSimpleButton(Client.getUIElement("btn_strengthen_HP") as SimpleButton);
			btnAP.wrapSimpleButton(Client.getUIElement("btn_strengthen_AP") as SimpleButton);
			btnDP.wrapSimpleButton(Client.getUIElement("btn_strengthen_DP") as SimpleButton);
			btnSP.wrapSimpleButton(Client.getUIElement("btn_strengthen_SP") as SimpleButton);
			btnStrengthen.wrapSimpleButton(Client.getUIElement("btn_strengthen") as SimpleButton);

			var group:ButtonGroup = new ButtonGroup();
			group.appendAll(btnHP,btnAP,btnDP,btnSP);

			
			btnHP.setSelected(_strengthen_p == 0);
			btnAP.setSelected(_strengthen_p == 1);
			btnDP.setSelected(_strengthen_p == 2);
			btnSP.setSelected(_strengthen_p == 3);
			
			btnHP.setText("");
			btnAP.setText("");
			btnDP.setText("");
			btnSP.setText("");
			btnStrengthen.setText("");
			
			var filter:GlowFilter;
			var font:ASFont = new ASFont("宋体", 12, false, false, false, false);
			filter = new GlowFilter(0x000000, 1, 2, 2, 10);
			labelName.setFont(font);
			labelName.setForeground(ASColor.WHITE);
			labelName.setTextFilters(new Array(filter));
			
			labelLevel.setFont(font);
			labelLevel.setForeground(ASColor.WHITE);
			labelLevel.setTextFilters(new Array(filter));
			
			labelOld.setFont(font);
			labelOld.setForeground(ASColor.WHITE);
			labelOld.setTextFilters(new Array(filter));
			
			labelNew.setFont(font);
			labelNew.setForeground(ASColor.WHITE);
			labelNew.setTextFilters(new Array(filter));
			
			labelCount.setFont(font);
			labelCount.setForeground(ASColor.WHITE);
			labelCount.setTextFilters(new Array(filter));
			
			labelRequireMoney.setFont(font);
			labelRequireMoney.setForeground(ASColor.YELLOW);
			labelRequireMoney.setTextFilters(new Array(filter));
			
			//labelCount.setText("可强化次数3/25");
			
			labelName.setText(_name);
			labelLevel.setText(_level.toString());
			//update();
			
			if(_strengthen_p == 0)
			{
				onBtnHP(null)
			}
			if(_strengthen_p == 1)
			{
				onBtnAP(null)
			}
			if(_strengthen_p == 2)
			{
				onBtnDP(null)
			}
			if(_strengthen_p == 3)
			{
				onBtnSP(null)
			}
			
		}
		
		public static var _frame:FrameStrengthen = null;
		static public var STRENGTHEN_HP:int = 0;
		static public var STRENGTHEN_AP:int = 1;
		static public var STRENGTHEN_DP:int = 2;
		static public var STRENGTHEN_SP:int = 3;

		static public var STRENGTHEN_TYPE_CHAR:int = 0;
		static public var STRENGTHEN_TYPE_PET:int = 1;
		static public var _name:String = "";
		static public var _level:int = 0;
		static public var _strengthen_p:int = 0;
		static public var _strengthen_type:int = 0;
		static public var _pet_index:int = 0;
		public static function show(name:String,m_nLevel:int,p:int = 0, strengthen_type:int = 0,pet_index:int = 0):void
		{
			if(isShowing())
			{
				hide();
				return;
			}
			if(_frame == null)
			{
				_frame = new FrameStrengthen();
				PanelMainHelp.getInstance().addChild(_frame);
			}
			_name = name;
			_level = m_nLevel;
			_strengthen_p = p;
			_strengthen_type = strengthen_type;
			_pet_index = pet_index;
			
			_frame.show();
			PanelMainHelp.getInstance().bringToTop(_frame);
			Client.getInstance().sendPacketLogic(12,[1,0]);
			Client.getInstance().sendPacketLogic(12,[2,0]);
			_frame.x = (Engine.W - _frame.width)/2;
			_frame.y = (Engine.H - _frame.height)/2;
			_frame.update();
			_frame.btnHP.setSelected(_strengthen_p == 0);
			_frame.btnAP.setSelected(_strengthen_p == 1);
			_frame.btnDP.setSelected(_strengthen_p == 2);
			_frame.btnSP.setSelected(_strengthen_p == 3);
			if(_strengthen_p == STRENGTHEN_HP)
			{
				_frame.onBtnHP(null)
			}
			if(_strengthen_p == STRENGTHEN_AP)
			{
				_frame.onBtnAP(null)
			}
			if(_strengthen_p == STRENGTHEN_DP)
			{
				_frame.onBtnDP(null)
			}
			if(_strengthen_p == STRENGTHEN_SP)
			{
				_frame.onBtnSP(null)
			}
			var _frameDest:JFrame = _strengthen_type == STRENGTHEN_TYPE_CHAR ?FrameChar._frame:FramePet._frame;

			_frame.setLocationXY((Engine.W - _frame.width)/2,(Engine.H - _frame.height)/2);
			
			var teamDestX:int = (Engine.W - (_frame.width+_frameDest.width))/2;
			var teamDestY:int = (Engine.H - _frameDest.height)/2;
			
			var listDestX:int = teamDestX + _frameDest.width;
			var listDestY:int = teamDestY + (_frameDest.height - _frame.height)/2;
			
			var tween:TweenLite = TweenLite.to(_frameDest, 0.3, {x:teamDestX, y:teamDestY});
			var list_tween:TweenLite = TweenLite.to(_frame, 0.3, {x:listDestX, y:listDestY});
		}
		public static function hide():void
		{
			if(isShowing())_frame.hide();
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
	}
}